home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / NNUTL101.ZIP / NNTEST1 / NNTEST1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  4.0 KB  |  74 lines

  1. /*--------------------------------------------------------------------------*
  2.  * Gregory Stevens                                                   7/1/93 *
  3.  *                               NNTEST1.C                                  *
  4.  *                                                                          *
  5.  *   This is a test file to test the nn*.c series with back-propagation.    *
  6.  * This file demonstrates learning of a linear function using 1 input, 1    *
  7.  * output net, with the output node having a linear activation function.    *
  8.  * For this file, the following parameters in the following files should be *
  9.  * set:                                                                     *
  10.  *      NNPARAMS.C : INPUT_LAYER_SIZE   1                                   *
  11.  *                   OUTPUT_LAYER_SIZE  1                                   *
  12.  *                   NUM_HIDDEN_LAYERS  0                                   *
  13.  *                                                                          *
  14.  *      NNINPUTS.C : NUM_PATTERNS  5                                        *
  15.  *                                                                          *
  16.  *      NNSTRUCT.C : InitNet()  ...should set output nodes as linear...     *
  17.  *                                                                          *
  18.  *      NNBKPROP.C : EPSILON 0.25  (recommended...this is what I used)      *
  19.  *                                                                          *
  20.  *  Everything else can be left unchanged.  The input files should each     *
  21.  * consist of five real numbers, where they are in matching order of input  *
  22.  * and desired output.  The output of this file simply lists the input,     *
  23.  * weight, output, threshhold, desired output.  It pauses at each training  *
  24.  * epoch.                                                                   *
  25.  *                                                                          *
  26.  *  NOTE: This file does not test the use of testing novel inputs after     *
  27.  * training, and thus does not test InitInPatterns(1) [with parameter 1].   *
  28.  * See nnsim1.c for that.                                                   *
  29.  *--------------------------------------------------------------------------*/
  30. #include "nnbkprop.c"                /* to chain it to the nn*.c utilities  */
  31. #include <math.h>                    /* for the exp() for logistic units    */
  32.  
  33. #define NUM_ITS 10                   /* iterations before it stops          */
  34.  
  35. /*  MAIN PROGRAM  */
  36.  
  37. void main()
  38. {
  39.   int Pattern;                         /* for looping through patterns   */
  40.   int Layer;                           /* for looping through layers     */
  41.   int LCV;                             /* for looping training sets      */
  42.   NNETtype Net;
  43.   PATTERNtype InPatterns, OutPattern;
  44.  
  45.   Net = InitNet( NUMNODES );            /* initializes the network        */
  46.   InPatterns = InitInPatterns(0);       /* loads input patterns from file */
  47.   OutPattern = InitOutPatterns();       /* loads output patterns from file*/
  48.  
  49.   for (LCV=0; (LCV < NUM_ITS); ++LCV)   /* loop through a training set    */
  50.     {
  51.       for (Pattern=0; (Pattern<NUM_PATTERNS); ++Pattern)
  52.          {
  53.             /* FORWARD PROPAGATION */
  54.             Net = UpDateInputAct( InPatterns, Pattern, Net );
  55.             for (Layer=1; (Layer<NUMLAYERS); ++Layer)
  56.               {
  57.                  Net = UpDateLayerAct( Net, Layer );
  58.               }
  59.  
  60.             /* OUTPUT PRINTS */
  61.             printf( "Input: %4.2f  ", Net.unit[0][0].state );
  62.             printf( "Weight:%4.2f  ", Net.unit[1][0].weights[0]);
  63.             printf( "Output:%4.2f  ", Net.unit[1][0].state  );
  64.             printf( "Thresh:%4.2f  ", Net.unit[1][0].thresh );
  65.             printf( "Goal: %4.2f\n",  OutPattern.p[Pattern][0] );
  66.  
  67.             /* BACKWARD PROPAGATION */
  68.             Net = UpDateWeightandThresh( Net, OutPattern, Pattern );
  69.          }
  70.       getc(stdin);           /* pause inbetween training epochs */
  71.       printf( "\n" );    /* skip a line  */
  72.     }
  73. }
  74.